﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Maticsoft.CodeHelper;
using Maticsoft.CodeEngine;
using System.CodeDom.Compiler;
using Microsoft.VisualStudio.TextTemplating;


namespace Codematic_CMT
{
    public partial class Form1 : Form
    {

        private string Templatefilename;
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                Run();
                //threadCode = new Thread(new ThreadStart(Run));
                //threadCode.Start();
            }
            catch
            {
            }

        }

        public void Run()
        {
            string temp_txt = txtTemplate.Text;

            TableHost host = new TableHost()
            {
                TableList = null,
                ViewList = null,
                ProcedureList = null,
                TemplateFile = "",
                DbName = "YHQ",
                TableName = "YHQ",
                TableDescription = "YHQ",
                NameSpace = "YHQ",
                Folder = "YHQ",
                DbHelperName = "YHQ",
                Fieldlist = null,
                Keys = null,
                FKeys = null,
                DbSet = null,
                BLLPrefix = "YHQ",
                BLLSuffix = "YHQ",
                DALPrefix = "YHQ",
                DALSuffix = "YHQ",
                DbType = "YHQ",
                ModelPrefix = "YHQ",
                ModelSuffix = "YHQ",
                ProcPrefix = "YHQ",
                ProjectName = "YHQ",
                TabNameRule = "YHQ",
            };

            CodeInfo _CodeInfo = GenerateCode(temp_txt, host);

            //测试输出 

            this.tabControl1.SelectedIndex = 1;
            this.txtcode.Text = _CodeInfo.Code;



        }
        public void 模板代码生成()
        {
            return;
            //CodeGenerator codeGenerator = new CodeGenerator()
            //CodeInfo codeInfo = new CodeInfo();
        }
        public CodeInfo GenerateCode(string input, TemplateHost host)
        {
            CodeInfo codeInfo = new CodeInfo();
            Engine engine = new Engine();
            codeInfo.Code = engine.ProcessTemplate(input, host);
            StringBuilder stringBuilder = new StringBuilder();

            try
            {
                foreach (CompilerError compilerError in host.ErrorCollection)
                {
                    stringBuilder.AppendLine(compilerError.ToString());
                }
            }
            catch (Exception ex)
            {

            }
            codeInfo.ErrorMsg = stringBuilder.ToString();
            codeInfo.FileExtension = host.FileExtension;
            return codeInfo;
        }

        private void btn_getFIle_Click(object sender, EventArgs e)
        {
            string TemplateFile = "";
            TemplateFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template\\TemplateFile\\mytemp.cmt");
            string input = File.ReadAllText(TemplateFile);

            txtTemplate.Text = input;


        }


    }
}
